package com.jdk8;

import java.util.Optional;

import org.junit.Test;

public class OptionalTest {
	@Test
	public void testName() throws Exception {

		Integer i1 = null;
		Integer i2 = new Integer(10);

		Optional<Integer> opt1 = Optional.ofNullable(i1);
		Optional<Integer> opt2 = Optional.ofNullable(i2);
		// System.out.println(opt1.get());//NoSuchElementException

		opt1.orElseGet(() -> new Integer(1));
		opt1.orElseThrow(() -> new NullPointerException());

		opt1 = opt1.empty();

		opt1.ifPresent((a) -> {
			System.out.println(1111);// 不会执行
		});
		opt2.ifPresent((a) -> {
			System.out.println(222); // 打印出222
		});

		System.out.println(opt1.isPresent());// false
		System.out.println(opt2.isPresent());// true

		Integer i3 = opt1.orElse(new Integer(11));// 如果为空，使用参数进行替换
		Integer i4 = opt2.orElse(new Integer(121));
		System.out.println(i3.intValue());
		System.out.println(i4.intValue());

	}
}
